﻿using DotNetDAL.Entity;
using DotNetDAL.Extensions;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace DotNetDAL.Connector
{
    public class ConnectorUtils
    {
        /// <summary>
        /// DataRow 转成 对象T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="row"></param>
        /// <returns></returns>
        public T ToObject<T>(DataRow row) where T : class
        {
            return row.ToObject<T>();
        }
        /// <summary>
        /// DataRow[] 转成 List<T>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="row"></param>
        /// <returns></returns>
        public List<T> ToList<T>(DataRow[] rows) where T : class
        {
            return rows.ToList<T>();
        }
        /// <summary>
        /// 将实体集合转换为DataTable
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="list">实体集合</param>
        /// <returns></returns>
        public DataTable ToDataTable<T>(List<T> list)
        {
            return list.ToDataTable();
        }
        /// <summary>
        /// 排序。排序字符串。像SQL 的order by 后面的内容。如：“AA”,“AA asc”，“AA desc,BB”
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="OrderBy">排序字符串。像SQL 的order by 后面的内容。如：“AA”,“AA asc”，“AA desc,BB”</param>
        /// <returns></returns>
        public List<T> SortBy<T>(List<T> list, string OrderBy) where T : class
        {
            return list.SortBy(OrderBy);
        }
        /// <summary>
        /// DataSet 转成 List
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public List<List<Dictionary<string, object>>> ToDictionaryList(DataSet ds)
        {
            return ds.ToDictionaryList();
        }
        /// <summary>
        /// 转换为字典集合
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<Dictionary<string, object>> ToDictionaryList(DataTable dt)
        {
            return dt.ToDictionaryList();
        }

        /// <summary>
        /// DataTable 转换为字典
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="majorKeyColumn">主键列名</param>
        /// <returns></returns>
        public Dictionary<string, T> ToDictionary<T>(DataTable dt, string majorKeyColumn) where T : class, new()
        {
            return dt.ToDictionary<T>(majorKeyColumn);
        }

        /// <summary>
        /// DataTable 转成 List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<T> ToList<T>(DataTable dt) where T : class
        {
            return dt.ToList<T>();
        }
        /// <summary>
        /// 第一行转换为T对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public T ToFirstOrDefault<T>(DataTable dt) where T : class
        {
            List<T> list = dt.ToList<T>();
            return list.Count > 0 ? list[0] : default(T);
        }

        /// <summary>
        /// DataTable 某列转成 String List
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ColumnName"></param>
        /// <returns></returns>
        public List<string> ToStringList(DataTable dt, string ColumnName)
        {
            return dt.ToStringList(ColumnName);
        }
        /// <summary>
        /// 生成分页数据
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="PageSize">每页大小</param>
        /// <param name="pageNumber">页码</param>
        /// <returns></returns>
        public PagingData ToPagingData(DataTable dt, int PageSize, int pageNumber)
        {
            return dt.ToPagingData(PageSize, pageNumber);
        }














    }
}
